home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / source / AudioEngine.cpp next >
C/C++ Source or Header  |  2005-11-07  |  5KB  |  248 lines

  1.  
  2. #include "FileLogger.h"
  3. #include "IniConfigReader.h"
  4. #include "AudioEngine.h"
  5.  
  6. namespace peon
  7. {
  8.     template<> AudioEngine* ISingleton<AudioEngine>::ms_Singleton = 0;
  9.  
  10.     AudioEngine* AudioEngine::getSingletonPtr(void)
  11.     {
  12.         return ms_Singleton;
  13.     }
  14.  
  15.     AudioEngine& AudioEngine::getSingleton(void)
  16.     {  
  17.  
  18.         assert( ms_Singleton );  
  19.  
  20.         return ( *ms_Singleton ); 
  21.     }
  22.  
  23.     AudioEngine::AudioEngine()
  24.     {
  25.         m_pALDevice = NULL;
  26.         m_pALContext = NULL;
  27.  
  28.         m_bEnableSound = true;
  29.         m_bEnableMusic = true;
  30.  
  31.         m_bAudioSupported = true;
  32.         m_iCurrentSlot = 0;
  33.  
  34.     }
  35.  
  36.     AudioEngine::~AudioEngine()
  37.     {
  38.         unloadEngine();
  39.     }
  40.  
  41.     bool AudioEngine::loadEngine( IniConfigReader* pConfig )
  42.     {
  43.         int audio_rate = 22050;
  44.         Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
  45.         int audio_channels = 2;
  46.         int audio_buffers = 4096;
  47.  
  48.  
  49.         // This is where we open up our audio device.
  50.         // Note that this section is for loading up SDL_Mixer for
  51.         // MIDI support
  52.         if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers))
  53.         {
  54.             FileLogger::getSingleton().logError("AudioEngine", "Error loading audio device");
  55.             m_bAudioSupported = false;
  56.             return false;
  57.         }
  58.  
  59.  
  60.         //alutInit(NULL, 0);
  61.         alGetError(); //clear error code
  62.  
  63.         //open a link to the audio hardware using the DirectSound3D
  64.         //underlay
  65.         m_pALDevice = alcOpenDevice((ALubyte*)"DirectSound3D");
  66.         if(m_pALDevice == NULL)
  67.         {
  68.             m_bAudioSupported = false;
  69.             return false;
  70.         }
  71.  
  72.         //Create a valid context
  73.         m_pALContext=alcCreateContext(m_pALDevice,NULL);
  74.  
  75.         //make it the current active context
  76.         alcMakeContextCurrent(m_pALContext);
  77.  
  78.  
  79.         m_iCurrentSlot = 0;
  80.         alGenBuffers(NUM_BUFFERS, m_uAudioBuffers);
  81.  
  82.         if(alGetError() != AL_NO_ERROR)
  83.         {
  84.             m_bAudioSupported = false;
  85.             return false;
  86.         }
  87.  
  88.         //position of the listener
  89.         ALfloat ListenerPos[] = { 0.0f, 0.0f, 0.0f };
  90.  
  91.         // Velocity of the listener.
  92.         ALfloat ListenerVel[] = { 0.0f, 0.0f, 0.0f };
  93.  
  94.         //Orientation of the listener. (first 3 elements are "at", second 3 are "up")
  95.         ALfloat ListenerOri[] = { 0.0f, 0.0f, -1.0f,  0.0f, 1.0f, 0.0f };
  96.  
  97.         alListenerfv(AL_POSITION,    ListenerPos);
  98.         alListenerfv(AL_VELOCITY,    ListenerVel);
  99.         alListenerfv(AL_ORIENTATION, ListenerOri);
  100.  
  101.         return true;
  102.     }
  103.  
  104.     void AudioEngine::unloadEngine()
  105.     {
  106.         Mix_HaltMusic();
  107.  
  108.         Mix_CloseAudio();
  109.  
  110.         //Get active context
  111.         m_pALContext = alcGetCurrentContext();
  112.  
  113.         //Get device for active context
  114.         m_pALDevice=alcGetContextsDevice(m_pALContext);
  115.  
  116.         //Disable context
  117.         alcMakeContextCurrent(NULL);
  118.  
  119.         //Release context(s)
  120.         alcDestroyContext(m_pALContext);
  121.  
  122.         //Close device
  123.         alcCloseDevice(m_pALDevice);
  124.  
  125.         alutExit();
  126.  
  127.     }
  128.  
  129.     Mix_Music* AudioEngine::loadMIDI( const String& strFilename )
  130.     {
  131.         Mix_Music* pMusicChunk = NULL;
  132.  
  133.         /* Actually loads up the music */
  134.         pMusicChunk = Mix_LoadMUS(strFilename.c_str());
  135.         
  136.         return( pMusicChunk );
  137.     }
  138.  
  139.     Mix_Chunk* AudioEngine::loadWAVChunk( const String& strFilename )
  140.     {
  141.         Mix_Chunk* pChunk = NULL;
  142.  
  143.         pChunk = Mix_LoadWAV( strFilename.c_str() );
  144.  
  145.         return( pChunk );
  146.  
  147.     }
  148.  
  149.     bool AudioEngine::loadAudioNode(const String& strWAV, AudioNode* pNode)
  150.     {
  151.         ALenum format;
  152.         ALsizei size;
  153.         ALvoid* data;
  154.         ALsizei freq;
  155.         ALboolean loop;
  156.         //String strTemp = strWAV;
  157.  
  158.         FILE*           oggFile;       // file handle
  159.         OggVorbis_File  oggStream;     // stream handle
  160.         vorbis_info*    vorbisInfo;    // some formatting data
  161.         vorbis_comment* vorbisComment; // user comments
  162.  
  163.         alGenSources( 1, &pNode->sound_source );
  164.  
  165.         if (alGetError() != AL_NO_ERROR)
  166.             return false;
  167.  
  168.         int pos = (int)strWAV.find( ".ogg", 0 );
  169.         if ( pos != String::npos )
  170.         {
  171.             int res;
  172.  
  173.             //we're loading an OGG file!!! 
  174.             if(!(oggFile = fopen(strWAV.c_str(), "rb")))
  175.                 return false;
  176.  
  177.             if((res = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
  178.             {
  179.                 fclose(oggFile);
  180.  
  181.                 return false;
  182.             }
  183.  
  184.             vorbisInfo = ov_info(&oggStream, -1);
  185.             vorbisComment = ov_comment(&oggStream, -1);
  186.  
  187.             if(vorbisInfo->channels == 1)
  188.                 format = AL_FORMAT_MONO16;
  189.             else
  190.                 format = AL_FORMAT_STEREO16;
  191.  
  192.  
  193.             alSourcef(pNode->sound_source, AL_ROLLOFF_FACTOR,  0.0f    );
  194.             alSourcei(pNode->sound_source, AL_SOURCE_RELATIVE, AL_TRUE );
  195.  
  196.         }
  197.         else
  198.         {
  199.             //we're loading a simple ol' WAV file
  200.             alutLoadWAVFile((char*)strWAV.c_str(), &format, &data, &size, &freq, &loop);
  201.             alBufferData(m_uAudioBuffers[m_iCurrentSlot], format, data, size, freq);
  202.             alutUnloadWAV(format, data, size, freq);
  203.  
  204.             pNode->sound_buffer = m_iCurrentSlot;
  205.         }
  206.         
  207.         
  208.  
  209.         
  210.  
  211.  
  212.  
  213.         m_iCurrentSlot++;
  214.         return true;
  215.     }
  216.  
  217.     void AudioEngine::setAudioNode(AudioNode* pNode)
  218.     {
  219.         ALuint source = pNode->sound_source;
  220.  
  221.         alSourcei (source, AL_BUFFER,   m_uAudioBuffers[pNode->sound_buffer] );
  222.         alSourcef (source, AL_PITCH,    1.0f     );
  223.         alSourcef (source, AL_GAIN,     1.0f     );
  224.         alSourcefv(source, AL_POSITION, pNode->sound_position );
  225.         alSourcefv(source, AL_VELOCITY, pNode->sound_velocity );
  226.  
  227.         if( pNode->sound_loop )
  228.             alSourcei (source, AL_LOOPING, AL_TRUE );
  229.         else
  230.             alSourcei (source, AL_LOOPING, AL_FALSE );
  231.  
  232.     }
  233.  
  234.     void AudioEngine::playAudioNode( AudioNode* pNode )
  235.     {
  236.         // Begin the source playing.
  237.         alSourcePlay( pNode->sound_source );
  238.  
  239.     }
  240.  
  241.     void AudioEngine::stopAudioNode( AudioNode* pNode )
  242.     {
  243.         alSourceStop( pNode->sound_source );
  244.     }
  245.  
  246. }
  247.  
  248.